<?php

class Kernel_Form_Element_Filter_Slug extends Kernel_Component 
    implements Kernel_Form_Element_Filter_Interface
{
    
    /**
     * Returns the result of filtering $value
     *
     * @param  mixed $value
     * @throws Kernel_firm_Element_Filter_Exception If filtering $value is impossible
     * @return mixed
     */
    public function filter($value, array $values = array()) 
    {
        if (!is_string($value)) {
            throw new Kernel_Form_Element_Filter_Exception(
                sprintf('Value must be of String type, %s given', gettype($value)));
        }
        
        $slug = '';

        for ($i=0; $i<strlen($value); $i++) {
            $char = preg_replace('/[^a-z0-9-]/', '', 
                strtr(strtolower($value[$i]), array(' ' => '-', '_' => '-')));
            $slug .= $char;
        }

        return trim($slug, '-');
    }
    
}
